package org.intellij.lang.assembly;

import com.intellij.ide.highlighter.custom.SyntaxTable;
import com.intellij.lang.Language;
import com.intellij.openapi.fileTypes.SingleLazyInstanceSyntaxHighlighterFactory;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import org.intellij.lang.assembly.highlighter.AssemblyHighlighter;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Properties;

/**
 * @author weijiancai
 * @version 1.0
 */
public class AssemblyLanguage extends Language {
    public static final AssemblyLanguage INSTANCE = new AssemblyLanguage();
    
    public AssemblyLanguage() {
        super("Assembly", "text/assembly");
        SyntaxHighlighterFactory.LANGUAGE_FACTORY.addExplicitExtension(this, new SingleLazyInstanceSyntaxHighlighterFactory() {

            @NotNull
            @Override
            protected SyntaxHighlighter createHighlighter() {
                SyntaxTable syntaxTable = new SyntaxTable();
                Properties properties = new Properties();
                try {
                    
                    properties.load(AssemblyLanguage.class.getResourceAsStream("/AssemblyHighlighter.properties"));

                    syntaxTable.setLineComment(properties.getProperty("LineComment"));
                    syntaxTable.setIgnoreCase(Boolean.valueOf(properties.getProperty("IgnoreCase")));
                    syntaxTable.setNumPostfixChars(properties.getProperty("NumPostfixChars"));
                    syntaxTable.setHasBraces(Boolean.valueOf(properties.getProperty("HasBraces")));
                    syntaxTable.setHasBrackets(Boolean.valueOf(properties.getProperty("HasBrackets")));
                    syntaxTable.setHasParens(Boolean.valueOf(properties.getProperty("HasParens")));
                    syntaxTable.setHasStringEscapes(Boolean.valueOf(properties.getProperty("HasStringEscapes")));
                    syntaxTable.setHexPrefix(properties.getProperty("HexPrefix"));

                    String keyword1 = properties.getProperty("Keyword1");
                    if (keyword1 != null && keyword1.length() > 0) {
                        for (String keyword : keyword1.split(",")) {
                            syntaxTable.addKeyword1(keyword);
                        }
                    }

                    String keyword2 = properties.getProperty("Keyword2");
                    if (keyword2 != null && keyword2.length() > 0) {
                        for (String keyword : keyword2.split(",")) {
                            syntaxTable.addKeyword2(keyword);
                        }
                    }

                    String keyword3 = properties.getProperty("Keyword3");
                    if (keyword3 != null && keyword3.length() > 0) {
                        for (String keyword : keyword3.split(",")) {
                            syntaxTable.addKeyword3(keyword);
                        }
                    }

                    String keyword4 = properties.getProperty("Keyword4");
                    if (keyword4 != null && keyword4.length() > 0) {
                        for (String keyword : keyword4.split(",")) {
                            syntaxTable.addKeyword4(keyword);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return new AssemblyHighlighter(syntaxTable);
            }
        });
    }
}
